<template>
  <div class="admin-wrapper">
    <el-container>
      <el-header>Header</el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu default-active="1-4-1"
                   class="el-menu-vertical-demo"
                   @open="handleOpen"
                   @close="handleClose"
                   :collapse="isCollapse">
            <template v-for="firstLayer in menuList">
              <el-submenu v-if="firstLayer.children"
                          :key="firstLayer.id"
                          :index="firstLayer.id">
                <!-- 一级菜单 -->
                <template slot="title">
                  <i :class="['a-AsideNav-itemIcon', 'icon' + firstLayer.imageIndex]"></i>
                  <span slot="title">{{firstLayer.name}}</span>
                </template>
                <el-menu-item v-for="secondLayer in firstLayer.children"
                              :key="secondLayer.id"
                              :index="secondLayer.id">
                  <!-- 二级菜单 -->
                  <template slot="title">
                    <i :class="['a-AsideNav-itemIcon', 'subIcon']"></i>
                    <span slot="title">{{secondLayer.name}}</span>
                  </template>
                  <!-- 三级菜单 -->
                  <!-- <el-menu-item v-for="thirdLayer in secondLayer.children"
                              :key="thirdLayer.id"
                              :index="thirdLayer.id">
                  <i class="el-icon-menu"></i>
                  <span slot="title">{{thirdLayer.name}}</span>
                </el-menu-item> -->
                </el-menu-item>
              </el-submenu>
              <el-menu-item v-if="!firstLayer.children"
                            :key="firstLayer.id"
                            :index="firstLayer.id">
                <!-- 一级菜单 -->
                <template slot="title">
                  <i :class="['a-AsideNav-itemIcon', 'icon' + firstLayer.imageIndex]"></i>
                  <span slot="title">{{firstLayer.name}}</span>
                </template>
                <el-menu-item v-for="secondLayer in firstLayer.children"
                              :key="secondLayer.id"
                              :index="secondLayer.id">
                  <!-- 二级菜单 -->
                  <template slot="title">
                    <i :class="['a-AsideNav-itemIcon', 'subIcon']"></i>
                    <span slot="title">{{secondLayer.name}}</span>
                  </template>
                  <!-- 三级菜单 -->
                  <!-- <el-menu-item v-for="thirdLayer in secondLayer.children"
                              :key="thirdLayer.id"
                              :index="thirdLayer.id">
                  <i class="el-icon-menu"></i>
                  <span slot="title">{{thirdLayer.name}}</span>
                </el-menu-item> -->
                </el-menu-item>
              </el-menu-item>
            </template>
          </el-menu>
        </el-aside>
        <router-view></router-view>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import { getMenu } from '../../api/token'
import { mapTree } from '../../utils'
export default {
  name: 'Admin',
  components: {

  },
  data() {
    return {
      menuList: [],
      isCollapse: false,
    }
  },
  created() {
    this.getMenuList();
  },
  methods: {
    handleOpen() {

    },
    handleClose() {

    },
    getMenuList() {
      getMenu().then((res) => {
        const { code, data } = res.data;
        if (code === 200) {
          data.unshift({ id: '01', name: '首页', parentId: 'root', imageIndex: '01', jspath: '/admin/home' });
          data.forEach((root) => {
            root.children &&
              mapTree(root.children, (item) => {
                item.key = item.id;
                item.children &&
                  (item.children = item.children.sort((a, b) => {
                    return Number(a.id) - Number(b.id);
                  }));
              });
          });
          this.menuList = data;
        }
      });
    }
  }
}

/* 
1、v-if、v-for使用点；
2、动态类名、样式写法；
 */
</script>

<style lang="scss" scoped>
.admin-wrapper {
  width: 100%;
  height: 100%;
  .el-container {
    height: 100%;
    .el-aside {
      .a-AsideNav-itemIcon {
        line-height: 2.5rem;
        width: 2.5rem;
        vertical-align: middle;
        float: left;
        text-align: center;
        position: relative;
        overflow: hidden;

        height: 100%;
        background-size: 20px 20px;
        background-position: center;
        margin: 0 0px;
        background-repeat: no-repeat;
      }
      .subIcon {
        background-image: url(../../assets/image/subIcon1.png);
      }
      .iconsearch_menu {
        background-image: url(../../assets/image/search_menu.png);
      }
      .icon01 {
        background-image: url(../../assets/image/home.png);
      }
      .icon0 {
        background-image: url(../../assets/image/002.png);
      }
      .icon1 {
        background-image: url(../../assets/image/003.png);
      }
      .icon2 {
        background-image: url(../../assets/image/projManager.png);
      }
      .icon3 {
        background-image: url(../../assets/image/tableManager.png);
      }
      .icon4 {
        background-image: url(../../assets/image/zonghe.png);
      }
      .icon5 {
        background-image: url(../../assets/image/07.png);
      }
      .icon6 {
        background-image: url(../../assets/image/settings.png);
      }
      .icon9 {
        background-image: url(../../assets/image/settings.png);
      }
      .icon7 {
        background-image: url(../../assets/image/setting.png);
      }
      .icon8 {
        background-image: url(../../assets/image/010.png);
      }
    }
  }
}
</style>